#pragma once

#ifndef TIMESERIES_H
#define TIMESERIES_H

#include "Date.h"

#include <map>
#include <vector>
#include <string>
#include <loki/SmartPtr.h>

using namespace std;
namespace SmartTrade
{
	template<typename T,typename DateKey>
	struct TimePair
	{
		DateKey DateTime;
		T StoredObj;
	};

	template<typename T,typename DateKey = Date>
	class TimeSeries
	{
		typedef T StoredType;
		typedef DateKey DateType;
		typedef TimePair<T,DateKey>* StoredPair;
		typedef Loki::SmartPtr<vector<StoredPair> > Ptr_StoredVector;

	public:
		TimeSeries()
		{
			pStoredVec_ = new vector<StoredPair>(40000); 
		}

		~TimeSeries()
		{
			Clear();
		}

		void Add(const StoredType p)
		{
			if (p != NULL)
			{
				StoredPair pair = new TimePair<T,DateKey>();
				pair->DateTime =  DateKey::Now();
				pair->StoredObj = p;
				pStoredVec_->push_back(pair);
			}
		}
		
		void Add(const DateType d,const StoredType p)
		{
			if(d != NULL && p != NULL)
			{
				StoredPair pair = new TimePair<T,DateKey>();
				pair->DateTime =  d;
				pair->StoredObj = p;
				pStoredVec_->push_back(pair);
			}
		}
		
		StoredType Ago(size_t index = 0)
		{
			size_t iPosition = 0;
			if( pStoredVec_->size() > index)
				iPosition = pStoredVec_->size() - index;

			return pStoredVec_->at(iPosition)->StoredObj;
		}

		size_t Size()
		{
			if(NULL == pStoredVec_)
				return 0;
			return pStoredVec_->size();
		}


		void Clear()
		{
			pStoredVec_->clear();
		}
	private:
		Ptr_StoredVector pStoredVec_;
	};

}
#endif // TIMESERIES_H